﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Application
open System.Security

// =======================================================
// Create, retrieve, update, delete items
// =======================================================

let list = Client.Context.Web.Lists.GetByTitle("Documents")
Client.Load list

// Create and Update item
let itemInfo = new ListItemCreationInformation()
itemInfo.LeafName <- "TestItem"
let item = list.AddItem(itemInfo)
Client.ExecuteQuery()

item.["ABC"] <- "Test"
item.Update()
Client.ExecuteQuery()

// Update Item
let upList = Client.Context.Web.Lists.GetByTitle("TestList")
Client.Load upList
let query= CamlQuery.CreateAllItemsQuery()
let listItems = upList.GetItems(query)
Client.Load listItems

// Retrieve items with metadata
Client.Context.LoadQuery(listItems.Include(fun (item:ListItem) -> item.["ABC"]))
Client.ExecuteQuery()

for spItem in listItems do
    let newName = spItem.["Title"].ToString() + spItem.["ABC"].ToString()
    spItem.["Title"] <- newName
    spItem.Update()
    Client.ExecuteQuery()

// Delete Item
let deleteItem = list.GetItemById(1)
deleteItem.DeleteObject()
Client.ExecuteQuery()
